package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.controller.util.Code;
import com.example.controller.util.Data;
import com.example.domain.User;
import com.example.domain.Volunteer;
import com.example.service.IActivityService;
import com.example.service.IVolunteerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/volunteers")
public class VolunteerController {

    @Autowired
    private IVolunteerService volunteerService;


    /**
     * 根据status判断是否为正式志愿者，并分类查询！
     * 0审核通过
     * 1正在审核
     * 2审核失败
     * @return
     */
    @GetMapping("/page/{status}/{page}/{size}")
    public Data getPageVolunteer(@PathVariable Integer status,@PathVariable Integer page,@PathVariable Integer size){
        IPage<Volunteer> pageVolunteer = volunteerService.getPageVolunteer(page, size,status);
        Integer code = pageVolunteer != null ? Code.Code_ok : Code.Code_err;
        String msg = pageVolunteer != null ? "查询成功" : "查询失败";
        return new Data(code,pageVolunteer, msg);
    }
    @GetMapping("{id}")
    public Data getVolunteerById(@PathVariable Integer id){
        LambdaQueryWrapper<Volunteer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Volunteer::getId,id);
        Volunteer volunteer=volunteerService.getOne(queryWrapper);
        Integer code = volunteer==null ? Code.Code_err :Code.Code_ok;
        String msg = volunteer == null ? "query_err" : "query_ok";
        return new Data(code,volunteer,msg);
    }


//    增，提交请求后需要审核
    @PostMapping
    public Data saveVolunteer(@RequestBody Volunteer volunteer){

        Volunteer volunteer1 = volunteerService.getVolunteer(volunteer.getPhone());
        if(volunteer1!=null){
            return  new Data(Code.Code_err,"用户已存在，请重新输入");
        }else {
            //提交审核，设置状态值为1
            volunteer.setStatus(1);
            boolean save = volunteerService.save(volunteer);
            Integer code = save ? Code.Code_ok :Code.Code_err ;
            String msg = save ? "已提交审核信息，将在2-3个工作日完成审核" : "提交错误";
            return new Data(code,msg);
        }
    }
//    更新状态
    @PutMapping("/pass/{id}/{status}")
    public Data pass(@PathVariable Integer id,@PathVariable Integer status){
        Volunteer volunteer = volunteerService.getById(id);
        volunteer.setStatus(status);
        boolean result = volunteerService.updateById(volunteer);
        Integer code = !result ? Code.Code_err :Code.Code_ok;
        String msg = !result ? "update_err" : "update_ok";
        return new Data(code,msg);
    }

//  删
    @DeleteMapping("/{id}")
    public Data deleteVolunteer(@PathVariable Integer id){
        boolean b = volunteerService.removeById(id);
        Integer code = !b ? Code.Code_err :Code.Code_ok;
        String msg = !b ? "delete_err" : "delete_ok";
        return new Data(code,msg);
    }

//   改
    @PutMapping
    public Data update( @RequestBody Volunteer volunteer){
        boolean updateById = volunteerService.updateById(volunteer);
        Integer code = !updateById ? Code.Code_err :Code.Code_ok;
        String msg = !updateById ? "delete_err" : "delete_ok";
        return new Data(code,msg);
    }

//    登录查询,查询单个信息
    @GetMapping("{name}/{phone}")
    public Data getOne(@PathVariable String name,@PathVariable String phone){
        Volunteer one = volunteerService.getOne(name, phone);
        if (one.getStatus() == 1){
            return new Data(201,"您的账户正在审核中！请稍后登录。");
        }
        if (one.getStatus() == 2){
            return new Data(201,"您的志愿信息未符合要求，请修改后重新注册！");
        }
        Integer code = one!=null ? Code.Code_ok : Code.Code_err;
        String msg= one!=null ? "登录成功" :"登录失败";
        String token = "welcome_volunteer";
        return new Data(code,one,msg,token);
    }
}
